﻿using System;
using System.Collections.Specialized;
using System.Configuration.Provider;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;

namespace Avaxo.Scripting
{
    internal abstract class DynamicLanguageEngineBase : ProviderBase, ILanguageEngine
    {
        private string languageShortName;

        #region Properties

        public string LanguageName
        {
            get { return Name; }
        }

        public string LanguageShortName
        {
            get { return languageShortName; }
        }


        public abstract Version Version { get; }

        public abstract ScriptEngine ScriptEngine { get; }

        public abstract IScriptModule CurrentModule { get; }

        public abstract ICodeSenseProvider CodeSenseProvider { get; }

        #endregion

        protected DynamicLanguageEngineBase()
        {
        }

        public override void Initialize( string name, NameValueCollection config )
        {
            if ( config == null )
            {
                throw new ArgumentNullException( "config" );
            }
            if ( string.IsNullOrEmpty( name ) )
            {
                name = "DynamicLanguageEngineBase";
            }
            if ( string.IsNullOrEmpty( config[ "description" ] ) )
            {
                config.Remove( "description" );
                config.Add( "description", "Base languange engine for Avaxo scripting." );
            }

            base.Initialize( name, config );

            config.Remove( "name" );
            config.Remove( "description" );

            languageShortName = config[ "languageShortName" ];
            CheckAttributeValue( "languageShortName", languageShortName );
            config.Remove( "languageShortName" );
        }

        protected static void CheckAttributeValue( string attributeName, string value )
        {
            if ( string.IsNullOrEmpty( value ) )
            {
                throw new ProviderException( attributeName + " not found." );
            }
        }

        public abstract bool IsExpressionValid( string expression );
        public abstract bool IsStatementComplete( string statement );
    }
}